// pages/search/search.js
// import { HTTP } from '../../utils/http-promise'
// const http = new HTTP()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hotSearch: ['vue', 'react', 'webpack', '小程序', 'HTML5', 'CSS3', 'Javascript', 'Angular'],
    lesson: [],
    showLesson: false,
    keyword: '',
    noData: false,
    loading: false,
    noMore: false,
    timer: 0,
    historySearch: wx.getStorageSync('historySearch') || []
  },
  goBack() {
    wx.navigateBack({
      delta: 2
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  getHotSearch() {
    // http.request({
    //   url: '/hot_search'
    // }).then(data => {
    //   this.setData({
    //     hotSearch: data
    //   })
    // })
  },
  onInput(e) {
    clearTimeout(this.data.timer)
    let timer = setTimeout(() => {
      console.log(e);
    }, 600)
    this.data.timer = timer
  },
  onConfirm(e) {
    // console.log(e)
    let keyword = e.detail.value || e.currentTarget.dataset.word
    if (keyword) {
      this.addHistorySearch(keyword)
      this.searchByKeyword(keyword)
      this.setData({
        keyword: keyword
      })
    }
  },
  searchByKeyword(word) {
    this.locked()
    // let start = this.data.lesson.length
    // http.request({
    //   url: `/query?q=${word}$start=${start}$size=6`
    // }).then(data => {
    //    this.setLesson(data)
    //    this.unLocked()
    // }, () => {
    //    this.unLocked()
    // })
  },
  setLesson(data) {
    console.log(data)
    this.data.total = data.total
    if (data.total === 0) {
      this.setData({
        showLesson: true,
        noData: true
      })
    } else {
      let lesson = this.data.lesson.concat(data.lesson)
      this.setData({
        lesson: lesson,
        showLesson: true
      })
    }
  },
  addHistorySearch(value) {
    let historySearch = wx.getStorageSync('historySearch') || []
    console.log(wx.getStorageSync('historySearch') || [])
    let has = historySearch.includes(value)
    if (has) return 
    let len = historySearch.length
    if (len >= 10) {
      historySearch.pop()
    }
    historySearch.unshift(value)
    console.log(historySearch);
    wx.setStorage({
      key: 'historySearch',
      data: historySearch,
      success: () => {
        this.setData({
          historySearch: historySearch
        })
      }
    })
  },
  removeStorage() {
    wx.removeStorageSync('historySearch')
    this.setData({
      historySearch: []
    })
  },
  onClear() {
    this.init()
  },
  init() {
    this.setData({
      lesson: [],
      showLesson: false,
      keyword: '',
      noData: false,
      noMore: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.init()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.isLoading()) {
      return
    }
    if (this.hasMore()) {
      let keyword = this.data.keyword
      this.searchByKeyword(keyword)
    } else {
      this.setData({
        noMore: true
      })
    }
  },
  hasMore() {
    return this.data.lesson.length >= this.data.total ? false : true
  },
  isLoading() {
    return this.data.loading ? true : false
  },
  locked() {
    // wx.showLoading({
    //   title: '正在加载'
    // })
    wx.showNavigationBarLoading()
    this.setData({
      loading: true
    })
  },
  unLocked() {
    // wx.hideLoading()
    wx.hideNavigationBarLoading()
    this.setData({
      loading: false
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})